package vaani.shabd;

import java.util.Vector;
import vaani.internal.ActivityType;
import vaani.internal.applicationmanagers.ApplicationManager;
import vaani.internal.WordTagPair;
import vaani.internal.applicationmanagers.FileManager;
import vaani.internal.applicationmanagers.InstantMessageManager;
import vaani.internal.applicationmanagers.MediaManager;

/**
 *
 * @author sourcemorph
 */

public class Analyzer {
    private Vector<WordTagPair> current;
    private Vector<ActivityType> activity;
    private Vector<String> arguments;

    public Analyzer() {
    }

    public ApplicationManager analyze(String userSentence) {
        activity = new Vector<ActivityType>();
        arguments = new Vector<String>();
        ActivityType currentActivity;
        ApplicationManager appMan = null;
        try {
            current = Tagger.tag(userSentence);

        } catch(Exception ex) {
            ex.printStackTrace();
        }

        /*to take care of the period at the end of the phrase, which becomes a part of the last wordtagpair*/
        if(current.lastElement().getWord().endsWith(".")) {
            WordTagPair tempPair = new WordTagPair(current.lastElement().getWord().substring(0, current.lastElement().getWord().length() - 1), current.lastElement().getTag());
            current.remove(current.lastElement());
            current.add(tempPair);
        }
        
        /*analyze all the words in the sentence sequentially*/
        for(WordTagPair w : current) {
            if(TagAnalyzer.isVerb(w.getTag())) {
                if((currentActivity = ActivityMiner.matchActivityVerb(w.getWord())) != null) {
                    //System.out.println(currentActivity.getNum());
                    activity.add(currentActivity);
                    /*functionality to be added here*/
                }
            }
            if(TagAnalyzer.isNoun(w.getTag()))
                //System.out.println(w.getWord());
                arguments.add(w.getWord());
        }
        /*first phase of processing based on the first phase of reading*/
        /*if there is more than one activity associated then print ambiguity*/
        if(activity.size() == 0) {
            System.out.println("Could not ascertain the activity from the sentence, sorry, please try again");
            return null;
        }
        else {
            if(activity.size() > 1) {
                System.out.println("The sentence seems to be ambiguous, try a more precise sentence please");
                return null;
            }
            else {
                /*based on the classification of the activity an application manager is invoked*/
                switch(activity.firstElement().getNum()) {
                    case 1:
                        appMan = new MediaManager(arguments);
                        break;
                    case 2:
                        /*software application manager*/
                        break;
                    case 3:
                        appMan = new FileManager(arguments);
                        break;
                    case 4:
                        //System.out.println("IMM called");
                        appMan = new InstantMessageManager(arguments);
                        break;
                    default:
                        System.out.println("Unexpected flow error");
                        return null;
                }
            }
        }
        return appMan;
    }

}
